feat: add jsonToHtmlAsync and generic toTree<T>() with React primitives#100
Draft
glassdimlygr wants to merge 4 commits intocontentstack:masterfrom
Draft
feat: add jsonToHtmlAsync and generic toTree<T>() with React primitives#100glassdimlygr wants to merge 4 commits intocontentstack:masterfrom
glassdimlygr wants to merge 4 commits intocontentstack:masterfrom
Conversation
6cbbd2d to
c2200a4
Compare
Add toRedactorAsync (exported as jsonToHtmlAsync) that supports customElementTypes handlers returning string | Promise<string>. Enables dynamic component resolution (e.g. await import()) before serialization. Children are resolved via Promise.all concurrently. Refactors shared logic (text processing, attr building, element node processing) into toRedactorHelpers.ts so both sync and async versions are thin recursive shells with no duplicated code. The existing sync jsonToHtml behavior is unchanged. New types: IJsonToHtmlAsyncElementTags, IJsonToHtmlAsyncOptions.
c2200a4 to
44da5fa
Compare
Add toReactTree.tsx which walks the JSON RTE document and returns ReactNode instead of HTML strings. Handlers receive (jsonBlock, children) and return ReactNode directly, enabling real React component rendering without renderToStaticMarkup. - New exports: jsonToReact, IJsonToReactOptions, IJsonToReactElementHandler, IJsonToReactTextHandler - React is a peer dependency (optional) and externalized from the bundle - Default handlers map all standard element types to JSX equivalents - Text mark handlers (bold, italic, etc.) wrap children in semantic elements
Replace React-specific toReactTree.tsx with two modules: - src/toTree.ts: Generic tree walker that accepts IJsonToTreeOptions<T> with caller-provided callbacks (createText, createLineBreak, combineChildren, etc.). No React dependency, no JSX, no defaults. - src/react.tsx: Separate entry point (@contentstack/json-rte-serializer/react) that provides ready-to-use React primitives, default element handlers, default text marks, and a jsonToReact() convenience wrapper. Consumers import from /react to get the batteries-included experience, or use toTree<T>() directly for Preact, Solid, Vue h(), etc. React is externalized only from the /react entry point. The core package has zero React dependency.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two new output modes to the serializer, both purely additive — existing
jsonToHtml/htmlToJson/jsonToMarkdownare untouched.jsonToHtmlAsync— async variant ofjsonToHtmlwhere handlers can returnPromise<string>toTree<T>()— generic, framework-agnostic tree walker that outputs any typeT/reactentry point — reference implementation oftoTree<ReactNode>()with default handlers (see discussion below)jsonToHtmlAsyncAsync version of
toRedactor. Children are resolved concurrently viaPromise.all. Handler results areawaited, so both sync and async return values work. No changes to existingjsonToHtml.toTree<T>()— generic tree walkerA framework-agnostic tree walker in
src/toTree.ts(~120 lines, zero dependencies). AcceptsIJsonToTreeOptions<T>:The walker handles recursion, text processing, and mark application. The caller provides all construction logic for their chosen output format.
/reactentry point — discussionThis PR includes
src/react.tsx, shipped as a separate entry point at@contentstack/json-rte-serializer/react. It provides:reactPrimitives—createText,createLineBreak,combineChildren,wrapTextAttrs,wrapTextStyle,keyElementdefaultElementTypes— handlers for all standard element types (p,h1–h6,a,img, tables, grid, etc.)defaultTextMarks—bold→<strong>,italic→<em>, etc.jsonToReact()— convenience wrapper that combines everythingReact is an optional peer dependency and is externalized from the bundle. Non-React consumers never touch this entry point.
Question for maintainers
Should
/reactlive in this repo, or should it be maintained externally?Arguments for including it:
toTree<T>()— the generic API is only useful if there are reference implementationsexportsentry point means it's fully tree-shakeable and non-React consumers never see it.tsxbuild)Arguments for keeping it external:
Happy to split this PR and keep only
jsonToHtmlAsync+toTree<T>()if you'd prefer/reactto live elsewhere. The included implementation serves as documentation either way — it shows exactly how to wire uptoTreefor a virtual DOM framework.What's NOT changed
jsonToHtml/toRedactor— untouchedhtmlToJson/fromRedactor— untouchedjsonToMarkdown— untouchedPackage structure